package com.prs.crm.action.crm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.LiaisonReason;
import com.prs.crm.service.crm.LiaisonReasonService;
import com.prs.crm.service.sys.LogService;

public class LiaisonReasonAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private LiaisonReason liaisonReason;
	private LiaisonReasonService liaisonReasonService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	
	@Action("listLiaisonReason")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(LiaisonReason.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getLiaisonReasonService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editLiaisonReason")
	public String edit() {
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="saveLiaisonReason",results={@Result(name="success",type="redirect",location="listLiaisonReason"),
			@Result(name="error",type="chain",location="editLiaisonReason")})
	public String save() {
		if(liaisonReason.getName()==null||"".equals(liaisonReason.getName().trim())){
			this.addActionError("联络理由不能为空");
			return ERROR;
		}if (this.getLogService().getSearchName(LiaisonReason.class, liaisonReason
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.setPrompt("提交联络理由成功!");
		this.getLiaisonReasonService().save(liaisonReason);
		return SUCCESS;
	}
	
	@Action(value="removeLiaisonReason",results={@Result(name="success",type="redirect",location="listLiaisonReason"),
			@Result(name="error",type="chain",location="listLiaisonReason")})
	public String remove() {
		if(ids == null) {
			this.addActionError("请选择要删除的联络原因");
			return ERROR;
		}
		for(Integer id : ids) {
			LiaisonReason reason = this.getLiaisonReasonService().get(id);
			if(reason.getLiaisons().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getLiaisonReasonService().batchRemove(ids);
		return SUCCESS;
	}
	
	public LiaisonReason getLiaisonReason() {
		return liaisonReason;
	}
	public void setLiaisonReason(LiaisonReason liaisonReason) {
		this.liaisonReason = liaisonReason;
	}
	public LiaisonReasonService getLiaisonReasonService() {
		return liaisonReasonService;
	}
	public void setLiaisonReasonService(LiaisonReasonService liaisonReasonService) {
		this.liaisonReasonService = liaisonReasonService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
